package com.tc.juc.pack08;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: tianchao
 * @Date: 2021/10/17 22:22
 * @Description: 测试定时任务
 */
@Slf4j
public class TestSchedule {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        //当前时间
        LocalDateTime now = LocalDateTime.now();

        LocalDateTime thursDay = now.withHour(22).withMinute(38).withSecond(0).withNano(0).with(DayOfWeek.SUNDAY);
        if (now.compareTo(thursDay)>0 ){
            thursDay = thursDay.plusWeeks(1);
        }
        long initialDelay = Duration.between(now, thursDay).toMillis();
        long period = 1000*60*60*24*7;
        pool.scheduleAtFixedRate(()->{
           log.info("running....");
        }, initialDelay, period, TimeUnit.MILLISECONDS);
    }
}
